<template>
  <div class="login">
    <div class="avatar">
      <img class="logo" src="../assets/logo.png" alt>
    </div>
    <div class="text">
      <p>
        <span>海川云谷固定资产管理云平台</span>
      </p>
    </div>
    <el-form ref="form" :model="form" class="container" :rules="rules">
      <el-form-item></el-form-item>
      <el-form-item prop="account">
        <el-input v-model="form.account" placeholder="账户" prefix-icon="myicon myicon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="密码"
          prefix-icon="myicon myicon-key"
          @keydown.native.enter="loginSubmit('form')"
        ></el-input>
      </el-form-item>
      <el-form-item class="forget-password">
        <router-link to="/ForgetPassword">忘记密码?</router-link>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="loginSubmit('form')">登录</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="register-btn" @click="registerSubmit('form')">
          <router-link to="/Register">还没有账号?立即注册</router-link>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
           
<script>
import { login } from "../api/index.js";

export default {
  data() {
    return {
      form: {
        account: "",
        password: ""
      },
      rules: {
        account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    loginSubmit(formName) {
      // 校验表单
      this.$refs[formName].validate(valide => {
        if (valide) {
          // 发送登录请求
          login(this.form).then(res => {
            console.log(this.form);
            console.log(res, 22);
            if (res.status === 200) {
              // 1. 存储后台返回的登录成功凭证token
              localStorage.setItem("mytoken", res.data.token);
              // 2. 使用vuex存储用户名
              this.$store.commit("setaccount", res.data.account);
              // 3. 跳转至首页
              this.$router.push({ name: "Home" });
            } else {
              // 如果失败，展示提示信息
              this.$message({
                type: "error",
                message: res.meta.msg
              });
            }
          });
        } else {
          console.log("校验不通过");
        }
      });
    }
  }
  /* created() {
    this.$axios({
      method: "post",
      url: "api",
      data: this.qs.stringify({
        //这里是发送给后台的数据
        account: this.account,
        password: this.password
      })
    })
      .then(response => {
        //这里使用了ES6的语法
        console.log(response); //请求成功返回的数据
      })
      .catch(error => {
        console.log(error); //请求失败返回的数据
      });
  } */
};
</script>
<style lang = 'scss' scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    180deg,
    rgba(0, 255, 255, 1) 0%,
    rgba(0, 255, 255, 1) 0%,
    rgba(153, 255, 153, 1) 100%,
    rgba(153, 255, 153, 1) 100%
  );
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    border-radius: 2%;
    text-align: left;
  }
  .logo {
    border-width: 0px;
    position: absolute;
    left: 25px;
    top: 10px;
    width: 65px;
    height: 63px;
  }
  .text {
    border-width: 0px;
    position: absolute;
    left: 100px;
    top: 37px;
    width: 398px;
    height: 40px;
    font-family: "Arial-BoldMT", "Arial Bold", "Arial";
    font-weight: 700;
    font-style: normal;
    font-size: 28px;
    color: #fff;
  }
  .login-btn {
    width: 100%;
  }
  .register-btn {
    width: 100%;
  }
  .forget-password {
    text-align: right;
  }
}
</style>